package org.lc.stk.web.controller.question;

import jakarta.validation.Valid;

import org.lc.stk.service.question.EssayQuestionService;
import org.lc.stk.web.dto.question.EssayQuestionDTO;
import org.lc.stk.web.response.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/api/v1/questions/essay")
@Tag(name = "问答题管理", description = "问答题的增删改查接口")
public class EssayQuestionController {

    private final EssayQuestionService essayQuestionService;

    public EssayQuestionController(EssayQuestionService essayQuestionService) {
        this.essayQuestionService = essayQuestionService;
    }

    @PostMapping
    @Operation(summary = "创建问答题")
    @PreAuthorize("hasAnyRole('TEACHER', 'ADMIN')")
    public ResponseEntity<ApiResponse<EssayQuestionDTO>> createQuestion(
            @Parameter(description = "问答题信息") @Valid @RequestBody EssayQuestionDTO questionDTO) {
        EssayQuestionDTO created = essayQuestionService.createQuestion(questionDTO);
        return ResponseEntity.ok(ApiResponse.success(created));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新问答题")
    @PreAuthorize("hasAnyRole('TEACHER', 'ADMIN')")
    public ResponseEntity<ApiResponse<EssayQuestionDTO>> updateQuestion(
            @Parameter(description = "问答题ID") @PathVariable Long id,
            @Parameter(description = "问答题信息") @Valid @RequestBody EssayQuestionDTO questionDTO) {
        EssayQuestionDTO updated = essayQuestionService.updateQuestion(id, questionDTO);
        return ResponseEntity.ok(ApiResponse.success(updated));
    }
}